package com.ruoyi.common.utils;

import com.ruoyi.common.utils.file.FileUtils;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ThumbnailsUtils {
    private static final Logger logger = LoggerFactory.getLogger(ThumbnailsUtils.class);


    /**
     * 通过BufferedImage图片流调整图片大小
     */
    public static void resizeImageOne(String originalPath, int targetWidth, int targetHeight, String savePath) throws Exception {
        try {
            originalPath = URLDecoder.decode(originalPath, "utf-8");
            savePath = URLDecoder.decode(savePath, "utf-8");

            System.out.println("Processing file: " + originalPath);

            BufferedImage originalImage = ImageIO.read(new File(originalPath));
            if (originalImage == null) {
                System.out.println("Failed to read image from: " + originalPath);
                return; // 或者继续处理其他文件
            }

            BufferedImage thumbnail = Thumbnails.of(originalImage)
//                    .scale(0.5)
                    .size(targetWidth, targetHeight)
//                    .keepAspectRatio(true)
                    .outputFormat("JPEG")
                    .asBufferedImage();

            if (thumbnail != null) {
                System.out.println("Thumbnail width: " + thumbnail.getWidth());
                System.out.println("Thumbnail height: " + thumbnail.getHeight());
            } else {
                System.out.println("Thumbnail is null.");
                return;
            }

            ImageIO.write(thumbnail, "jpg", new File(savePath));
            System.out.println("Thumbnail saved at: " + savePath);
        } catch (IOException e) {
            System.out.println("IO exception while processing: " + originalPath);
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("An error occurred while processing: " + originalPath);
            e.printStackTrace();
        }


    }

    public static void resizeImages(String originalPath, String targetFilePath) throws Exception {
        originalPath = URLDecoder.decode(originalPath, "utf-8");
        targetFilePath = URLDecoder.decode(targetFilePath, "utf-8");
        File folder = new File(originalPath);
        File[] listOfFiles = folder.listFiles();
        //目标文件没有创建文件夹
        File outputFile = new File(targetFilePath);
        File outputDir = outputFile.getAbsoluteFile();
        // 如果文件夹不存在，则创建文件夹
        if (!outputDir.exists()) {
            outputDir.mkdirs();
        }
        if (listOfFiles == null) {
            System.out.println("The directory is empty or it does not exist.");
            return;
        }
        for (File file : listOfFiles) {
            if (file.isFile()) {
                String savePath = targetFilePath + "/" + FileUtils.getName(file.getAbsolutePath());
                resizeImageOne(file.getAbsolutePath(), 300, 300, savePath);
            } else if (file.isDirectory()) {
                System.out.println("Directory: " + file.getAbsolutePath());
            }
        }
    }

    public static void resizeImagesAll(String originalPath, String targetPath) throws IOException {
        originalPath = URLDecoder.decode(originalPath, "utf-8");
        // 指定要遍历的根目录路径
        Path rootPath = Paths.get(originalPath);
        // 遍历根目录下的所有文件和文件夹
        ExecutorService executorService = Executors.newFixedThreadPool(40);
        Files.walk(rootPath)
                .filter(Files::isRegularFile) // 过滤出普通文件
                .forEach(path -> {

                    executorService.submit(() -> {
                        try {
                            String localPath = targetPath + "\\最新" + path.getParent().toString().substring(targetPath.length());
                            File outputDir = new File(localPath);
                            // 如果文件夹不存在，则创建文件夹
                            if (!outputDir.exists()) {
                                outputDir.mkdirs();
                            }
                            String savePath = localPath + "\\" + path.getFileName();
                            Path tarSavePath = Paths.get(savePath);
                            if (!Files.exists(tarSavePath)) {
                                resizeImageOne(path.toString(), 1024, 1024, savePath);
                            }
                        } catch (Exception e) {
                            System.out.println(e);

                        }
                    });

                });
    }


    /**
     * BufferedImage图片流转byte[]数组
     */
    public static byte[] imageToBytes(BufferedImage bImage) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(bImage, "jpg", out);
        } catch (IOException e) {
            logger.error("错误信息: ", e);
        }
        return out.toByteArray();
    }


    /**
     * byte[]数组转BufferedImage图片流
     */
    private static BufferedImage bytesToBufferedImage(byte[] ImageByte) {
        ByteArrayInputStream in = new ByteArrayInputStream(ImageByte);
        BufferedImage image = null;
        try {
            image = ImageIO.read(in);
        } catch (IOException e) {
            logger.error("错误信息: ", e);
        }
        return image;
    }

    public static Boolean delAllImage(String targetPath) throws Exception {
        List<String> imageTypeList = Lists.newArrayList();
        imageTypeList.add("bmp");
        imageTypeList.add("gif");
        imageTypeList.add("jpg");
        imageTypeList.add("jpeg");
        imageTypeList.add("png");

        // 指定要遍历的根目录路径
        Path rootPath = Paths.get(targetPath);
        Files.walk(rootPath)
                .filter(Files::isRegularFile) // 过滤出普通文件
                .forEach(path -> {
                    File file = path.toFile();
                    String extension = FilenameUtils.getExtension(file.getName());

                    if (file.isFile() && imageTypeList.contains(extension.toLowerCase())) {
                        // 如果是图片文件，执行删除操作
                        logger.info("删除文件{}", file.getName());
                        file.delete();
                    }
                });
        logger.info("路径：{}删除完成", targetPath);
        return Boolean.TRUE;
    }

    public static Boolean delAlltxt(String targetPath) throws Exception {
        List<String> imageTypeList = Lists.newArrayList();
        imageTypeList.add("txt");
        imageTypeList.add("doc");
        imageTypeList.add("docx");
        imageTypeList.add("xls");
        imageTypeList.add("xlsx");
        imageTypeList.add("pdf");
        // 指定要遍历的根目录路径
        Path rootPath = Paths.get(targetPath);
        Files.walk(rootPath)
                .filter(Files::isRegularFile) // 过滤出普通文件
                .forEach(path -> {
                    File file = path.toFile();
                    String extension = FilenameUtils.getExtension(file.getName());

                    if (file.isFile() && imageTypeList.contains(extension.toLowerCase())) {
                        // 如果是图片文件，执行删除操作
                        logger.info("删除文件{}", file.getName());
                        file.delete();
                    }
                });
        logger.info("路径：{}删除完成", targetPath);
        return Boolean.TRUE;
    }

    public static void updateFileFommat(String targetPath) throws IOException {
        List<String> imageTypeList = Lists.newArrayList();
        imageTypeList.add("txt");
        imageTypeList.add("doc");
        imageTypeList.add("docx");
        imageTypeList.add("xls");
        imageTypeList.add("xlsx");
        imageTypeList.add("pdf");
        Path rootPath = Paths.get(targetPath);
        AtomicInteger count = new AtomicInteger();
        Files.walk(rootPath)
                .filter(Files::isRegularFile) // 过滤出普通文件
                .forEach(path -> {

                    File file1 = path.toFile();
                    String extension = FilenameUtils.getExtension(file1.getName());
                    if (imageTypeList.contains(extension.toLowerCase())) {
                        // 如果是图片文件，执行删除操作
                        try {
                            File file = new File(file1.getAbsolutePath());
                            BufferedReader reader = new BufferedReader(new FileReader(file));
                            StringBuilder sb = new StringBuilder();
                            String line;
                            List<String> stringList = Lists.newArrayList();
                            while ((line = reader.readLine()) != null) {
                                sb.append(line).append("\n");
                                if (StringUtils.isNotEmpty(line) && !"\n".equals(line)) {
                                    stringList.add(line);
                                }

                            }
                            reader.close();

                            String content = sb.toString();
                            String zhAnalysis = content.replaceAll("\n", "");
                            if (StringUtils.isNotEmpty(zhAnalysis) && !zhAnalysis.equals("null")) {

                                String tag = "";
                                String zhAnalysisTrans = "";
                                String enAnalysis = "";

                                if (stringList.size() >= 3) {
                                    tag = stringList.get(0);
                                    zhAnalysisTrans = stringList.get(1);
                                    enAnalysis = stringList.get(2);
                                } else if (stringList.size() == 2) {
                                    //特别注意 解析文件格式 为 标题 和 中文描述
                                    zhAnalysisTrans = stringList.get(0);
                                    enAnalysis = stringList.get(1);
                                } else if (stringList.size() == 1) {
                                    zhAnalysisTrans = stringList.get(0);

                                }
//                                if (StringUtils.isEmpty(enAnalysis)) {
//                                    count.getAndIncrement();
//                                    GT g = GT.getInstance();
//                                    enAnalysis = g.translateText(zhAnalysisTrans, "zh_cn", "en");
//                                    System.out.println("次数：" + count + "翻译后" + enAnalysis);
//                                }

                                StringBuffer buffer = new StringBuffer(tag.trim());
                                if (StringUtils.isNotEmpty(tag)) {

                                    buffer.append(System.lineSeparator()).append(zhAnalysisTrans.trim()).append(System.lineSeparator()).append(SentenceFormatter.capitalizeFirstLetter(enAnalysis.trim()));

                                } else {
                                    buffer.append(zhAnalysisTrans.trim()).append(System.lineSeparator()).append(SentenceFormatter.capitalizeFirstLetter(enAnalysis.trim()));
                                }
                                if (countsChinese(zhAnalysisTrans) < 30) {
                                    String path2 = "E:\\workdata\\码诚科技\\采集翻译\\正式对接\\王芳";
                                    String localPath = path2 + "\\字数小30" + path.getParent().toString().substring(path2.length());
                                    File outputDir = new File(localPath);
                                    // 如果文件夹不存在，则创建文件夹
                                    if (!outputDir.exists()) {
                                        outputDir.mkdirs();
                                    }
                                    String savePath = localPath + "\\" + path.getFileName();
                                    File file2 = new File(savePath);
                                    org.apache.commons.io.FileUtils.writeStringToFile(file2, buffer.toString(), "UTF-8");

                                }
                                org.apache.commons.io.FileUtils.writeStringToFile(file1, buffer.toString(), "UTF-8");
                                System.out.println("文档：" + file1.getAbsolutePath());
                            } else {

                                System.out.println("空的文件：" + file.getAbsolutePath());

                            }


                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                });

    }

    public static void collectionTranslate(String targetPath) throws Exception {
        Map<String, String> originalMap = new HashMap<>();
        Map<String, String> pathMap = new HashMap<>();

        Path rootPath = Paths.get(targetPath);
        Files.walk(rootPath)
                .filter(Files::isRegularFile) // 过滤出普通文件
                .forEach(path -> {
                    File file1 = path.toFile();
                    try {
                        File file = new File(file1.getAbsolutePath());
                        BufferedReader reader = new BufferedReader(new FileReader(file));
                        StringBuilder sb = new StringBuilder();
                        String line;
                        List<String> stringList = Lists.newArrayList();
                        while ((line = reader.readLine()) != null) {
                            sb.append(line).append("\n");
                            if (StringUtils.isNotEmpty(line) && !"\n".equals(line)) {
                                stringList.add(line);
                            }
                        }
                        reader.close();
                        String content = sb.toString();
                        String zhAnalysis = content.replaceAll("\n", "");
                        if (StringUtils.isNotEmpty(zhAnalysis) && !zhAnalysis.equals("null")) {
                            String zhAnalysisTrans = "";
                            String enAnalysis = "";
                            //特别注意 解析文件格式 为 标题 和 中文描述
                            if (stringList.size() >= 3) {
                                zhAnalysisTrans = stringList.get(1);
                                enAnalysis = stringList.get(2);
                            } else if (stringList.size() == 2) {
//                                tag = stringList.get(0);
                                zhAnalysisTrans = stringList.get(0);
                                enAnalysis = stringList.get(1);
                            } else if (stringList.size() == 1) {
                                zhAnalysisTrans = stringList.get(0);

                            }
//                            去人种肤色
                            if (zhAnalysisTrans.contains("白种")) {
                                zhAnalysisTrans = zhAnalysisTrans.replaceAll("白种", "");
                            } else if (zhAnalysisTrans.contains("黑种")) {
                                zhAnalysisTrans = zhAnalysisTrans.replaceAll("黑种", "");
                            } else if (zhAnalysisTrans.contains("黄种")) {
                                zhAnalysisTrans = zhAnalysisTrans.replaceAll("黄种", "");
                            } else if (zhAnalysisTrans.contains("棕种")) {
                                zhAnalysisTrans = zhAnalysisTrans.replaceAll("棕种", "");
                            } else if (zhAnalysisTrans.contains("白肤色")) {
                                zhAnalysisTrans = zhAnalysisTrans.replaceAll("白肤色", "");
                            } else if (zhAnalysisTrans.contains("黑肤色")) {
                                zhAnalysisTrans = zhAnalysisTrans.replaceAll("黑肤色", "");
                            } else if (zhAnalysisTrans.contains("黄肤色")) {
                                zhAnalysisTrans = zhAnalysisTrans.replaceAll("黄肤色", "");
                            } else if (zhAnalysisTrans.contains("棕肤色")) {
                                zhAnalysisTrans = zhAnalysisTrans.replaceAll("棕肤色", "");
                            }

                            UUID uuid = UUID.randomUUID();
                            if (StringUtils.isEmpty(enAnalysis)) {
                                originalMap.put(uuid.toString(), zhAnalysisTrans);
                                System.out.println("添加文件："+path.toString());
                            }
                            pathMap.put(uuid.toString(), path.toString());

                        } else {
                            System.out.println("空的文件：" + file.getAbsolutePath());
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                });
        Set<List<String>> listSet = new HashSet<>();
        List<String> strList = Lists.newArrayList();
        for (Map.Entry<String, String> entry : originalMap.entrySet()) {
            StringBuffer sb = new StringBuffer();
            sb.append(entry.getKey()).append("##").append(entry.getValue());
            strList.add(sb.toString());

            // 当 tempMap 的大小达到 20 时，保存并创建新的 tempMap
            if (strList.size() == 150) {
                listSet.add(new ArrayList<>(strList)); // 加入 Set
                strList.clear(); // 清空临时 List
            }
        }

        // 处理剩余的元素
        if (!strList.isEmpty()) {
            listSet.add(new ArrayList<>(strList));
        }

        googleTranlate(listSet, pathMap);
        System.out.println("程序执行完毕！！！！！！");
    }


    public static Map<String, String> googleTranlate(Set<List<String>> listSet, Map<String, String> pathMap) throws Exception {
        Map<String, String> resutlMap = new HashMap<>();
        // 打印结果
        AtomicInteger count = new AtomicInteger();
        for (List<String> list : listSet) {
            String zhTrains = String.join("&&&", list);
            count.getAndIncrement();
            GT g = GT.getInstance();
            String enAnalysis = g.translateText(zhTrains, "zh_cn", "en");
            System.out.println("次数：" + count + "翻译后" + enAnalysis);
            String[] keyValues = enAnalysis.split("&&&");
            for (String str : keyValues) {
                String[] strs = str.split("##");
                String baseName = strs[0];
                String enTranslate = strs[1];
                String path = pathMap.get(baseName);
                if (StringUtils.isNotEmpty(path)) {

                    updateFileFommatTranslate(path, enTranslate);
                }
            }
        }
        return resutlMap;
    }

    public static void updateFileFommatTranslate(String targetPath, String enTranslate) throws Exception {


        List<String> imageTypeList = Lists.newArrayList();
        imageTypeList.add("txt");
        imageTypeList.add("doc");
        imageTypeList.add("docx");
        imageTypeList.add("xls");
        imageTypeList.add("xlsx");
        imageTypeList.add("pdf");


        File file1 = new File(targetPath);
        String extension = FilenameUtils.getExtension(file1.getName());
        if (imageTypeList.contains(extension.toLowerCase())) {
            // 如果是图片文件，执行删除操作
            try {
                File file = new File(file1.getAbsolutePath());
                BufferedReader reader = new BufferedReader(new FileReader(file));
                StringBuilder sb = new StringBuilder();
                String line;
                List<String> stringList = Lists.newArrayList();
                while ((line = reader.readLine()) != null) {
                    sb.append(line).append("\n");
                    if (StringUtils.isNotEmpty(line) && !"\n".equals(line)) {
                        stringList.add(line);
                    }

                }
                reader.close();

                String content = sb.toString();
                String zhAnalysis = content.replaceAll("\n", "");
                if (StringUtils.isNotEmpty(zhAnalysis) && !zhAnalysis.equals("null")) {

                    String tag = "";
                    String zhAnalysisTrans = "";
                    String enAnalysis = "";

                    if (stringList.size() >= 3) {
                        tag = stringList.get(0);
                        zhAnalysisTrans = stringList.get(1);
                        enAnalysis = stringList.get(2);
                    } else if (stringList.size() == 2) {
                        zhAnalysisTrans = stringList.get(0);
                        enAnalysis = stringList.get(1);

                    } else if (stringList.size() == 1) {
                        zhAnalysisTrans = stringList.get(0);

                    }
                    if (StringUtils.isEmpty(enAnalysis)) {
                        enAnalysis = enTranslate;

                    }

                    StringBuffer buffer = new StringBuffer(tag.trim());
                    if (StringUtils.isNotEmpty(tag)) {

                        buffer.append(System.lineSeparator()).append(zhAnalysisTrans.trim()).append(System.lineSeparator()).append(SentenceFormatter.capitalizeFirstLetter(enAnalysis.trim()));

                    } else {
                        buffer.append(zhAnalysisTrans.trim()).append(System.lineSeparator()).append(SentenceFormatter.capitalizeFirstLetter(enAnalysis.trim()));
                    }
                    if (countsChinese(zhAnalysisTrans) < 30) {
                        String path2 = "E:\\workdata\\码诚科技\\采集翻译\\正式对接\\王芳";
                        String localPath = path2 + "\\字数小30" + file.getParent().toString().substring(path2.length());
                        File outputDir = new File(localPath);
                        // 如果文件夹不存在，则创建文件夹
                        if (!outputDir.exists()) {
                            outputDir.mkdirs();
                        }
                        String savePath = localPath + "\\" + file.getName();
                        File file2 = new File(savePath);
                        org.apache.commons.io.FileUtils.writeStringToFile(file2, buffer.toString(), "UTF-8");

                    }
                    org.apache.commons.io.FileUtils.writeStringToFile(file1, buffer.toString(), "UTF-8");
                    System.out.println("文档：" + file1.getAbsolutePath());
                } else {

                    System.out.println("空的文件：" + file.getAbsolutePath());
                }


            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }


    }

    public static void delEnglist(String targetPath) throws IOException {
        List<String> imageTypeList = Lists.newArrayList();
        imageTypeList.add("txt");
        imageTypeList.add("doc");
        imageTypeList.add("docx");
        imageTypeList.add("xls");
        imageTypeList.add("xlsx");
        imageTypeList.add("pdf");
        Path rootPath = Paths.get(targetPath);
        AtomicInteger count = new AtomicInteger();
        Files.walk(rootPath)
                .filter(Files::isRegularFile) // 过滤出普通文件
                .forEach(path -> {

                    File file1 = path.toFile();
                    String extension = FilenameUtils.getExtension(file1.getName());
                    if (imageTypeList.contains(extension.toLowerCase())) {
                        // 如果是图片文件，执行删除操作
                        try {
                            File file = new File(file1.getAbsolutePath());
                            BufferedReader reader = new BufferedReader(new FileReader(file));
                            StringBuilder sb = new StringBuilder();
                            String line;
                            List<String> stringList = Lists.newArrayList();
                            while ((line = reader.readLine()) != null) {
                                sb.append(line).append("\n");
                                if (StringUtils.isNotEmpty(line) && !"\n".equals(line)) {
                                    stringList.add(line);
                                }

                            }
                            reader.close();

                            String content = sb.toString();
                            String zhAnalysis = content.replaceAll("\n", "");
                            if (StringUtils.isNotEmpty(zhAnalysis) && !zhAnalysis.equals("null")) {
                                String tag = "";
                                String zhAnalysisTrans = "";
                                if (stringList.size() >= 3) {
                                    tag = stringList.get(0);
                                    zhAnalysisTrans = stringList.get(1);
                                } else if (stringList.size() == 2) {
                                    zhAnalysisTrans = stringList.get(0);
                                } else if (stringList.size() == 1) {
                                    zhAnalysisTrans = stringList.get(0);

                                }

                                StringBuffer buffer = new StringBuffer(tag.trim());
                                if (StringUtils.isNotEmpty(tag)) {

                                    buffer.append(System.lineSeparator()).append(zhAnalysisTrans.trim());//.append(System.lineSeparator()).append(SentenceFormatter.capitalizeFirstLetter(enAnalysis.trim()));
//                                    buffer.append(System.lineSeparator()).append(zhAnalysisTrans.trim());//.append(System.lineSeparator()).append(SentenceFormatter.capitalizeFirstLetter(enAnalysis.trim()));

                                } else {
                                    buffer.append(zhAnalysisTrans.trim());//.append(System.lineSeparator()).append(SentenceFormatter.capitalizeFirstLetter(enAnalysis.trim()));
                                }

                                org.apache.commons.io.FileUtils.writeStringToFile(file1, buffer.toString(), "UTF-8");
                                System.out.println("文档：" + file1.getAbsolutePath());
                            } else {

                                System.out.println("空的文件：" + file.getAbsolutePath());

                            }


                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                });

    }

    /**
     * 专业场景格式化
     *
     * @param targetPath
     * @throws IOException
     */
    public static void zycjFormat(String targetPath) throws IOException {
        List<String> imageTypeList = Lists.newArrayList();
        imageTypeList.add("txt");
        imageTypeList.add("doc");
        imageTypeList.add("docx");
        imageTypeList.add("xls");
        imageTypeList.add("xlsx");
        imageTypeList.add("pdf");
        Path rootPath = Paths.get(targetPath);
        Files.walk(rootPath)
                .filter(Files::isRegularFile) // 过滤出普通文件
                .forEach(path -> {

                    File file1 = path.toFile();
                    String extension = FilenameUtils.getExtension(file1.getName());
                    if (imageTypeList.contains(extension.toLowerCase())) {
                        // 如果是图片文件，执行删除操作
                        try {
                            File file = new File(file1.getAbsolutePath());
                            BufferedReader reader = new BufferedReader(new FileReader(file));
                            StringBuilder sb = new StringBuilder();
                            String line;
                            List<String> stringList = Lists.newArrayList();
                            while ((line = reader.readLine()) != null) {
                                sb.append(line).append("\n");
                                if (StringUtils.isNotEmpty(line) && !"\n".equals(line)) {
                                    stringList.add(line);
                                }

                            }
                            reader.close();

                            String content = sb.toString();
                            String zhAnalysis = content.replaceAll("\n", "");
                            if (StringUtils.isNotEmpty(zhAnalysis) && !zhAnalysis.equals("null")) {
                                String tag = "";
                                String zhAnalysisTrans = "";
                                String enAnalysis = "";
                                if (stringList.size() >= 3) {
                                    tag = stringList.get(0);
                                    zhAnalysisTrans = stringList.get(1);
                                    enAnalysis = stringList.get(2);
                                } else if (stringList.size() == 2) {
                                    zhAnalysisTrans = stringList.get(0);
                                } else if (stringList.size() == 1) {
                                    zhAnalysisTrans = stringList.get(0);

                                }
                                if (StringUtils.isNotEmpty(tag) && tag.contains("-")) {
                                    String[] strs = tag.split("-");
                                    if (strs != null) {
                                        tag = strs[1];
                                    }
                                }
                                StringBuffer buffer = new StringBuffer(tag.trim());
                                if (StringUtils.isNotEmpty(tag)) {

                                    buffer.append(System.lineSeparator()).append(zhAnalysisTrans.trim()).append(System.lineSeparator()).append(SentenceFormatter.capitalizeFirstLetter(enAnalysis.trim()));
//                                    buffer.append(System.lineSeparator()).append(zhAnalysisTrans.trim());//.append(System.lineSeparator()).append(SentenceFormatter.capitalizeFirstLetter(enAnalysis.trim()));

                                } else {
                                    buffer.append(zhAnalysisTrans.trim()).append(System.lineSeparator()).append(SentenceFormatter.capitalizeFirstLetter(enAnalysis.trim()));
                                }

                                org.apache.commons.io.FileUtils.writeStringToFile(file1, buffer.toString(), "UTF-8");
                                System.out.println("文档：" + file1.getAbsolutePath());
                            } else {

                                System.out.println("空的文件：" + file.getAbsolutePath());

                            }


                        } catch (IOException e) {
                            System.out.println("报错链接" + path.toString());
                            e.printStackTrace();
                        } catch (Exception e) {
                            System.out.println("报错链接" + path.toString());
                            throw new RuntimeException(e);
                        }
                    }
                });

    }

    /***
     * 统计字符串中汉字和数字的数目
     * @param str
     * @return
     */
    public static int countsChinese(String str) {
        int count = 0;
        Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]");//unicode的中文编码表，第一个“4e00”，最后一个“9fa5”
        Matcher m = p.matcher(str);
        while (m.find()) {
            count++;
        }
        return count;
    }

    public static void main(String[] args) throws Exception {
//        resizeImages("D:\\素材\\图库\\人物\\素人\\半夏微凉\\28岁的我","D:\\素材\\图库\\人物\\素人\\半夏微凉\\28岁的我\\压缩");
       // String str ="{\"requestId\":\"55ad265f-8d7f-9fb7-9b50-c033f9f54f5a\",\"usage\":{\"input_tokens\":352,\"output_tokens\":134},\"output\":{\"choices\":[{\"finish_reason\":\"stop\",\"message\":{\"role\":\"assistant\",\"content\":[{\"text\":\"这张照片显示了一个繁忙的交叉路口。我们可以看到多辆汽车、摩托车和自行车在道路上行驶，遵守交通规则并注意安全。\\n\\n道路左侧有一条人行横道线，并且可以看到一个穿着红色衣服的人正在过马路。右侧有一个黄色的道路标志或指示牌，可能提供一些关于该区域的信息或者警告驾驶员前方有特定的情况需要注意。\\n此外，在图像中还可以观察到几个路标和其他基础设施元素，如红绿灯等，这些都为车辆和行人提供了方向指引以及管理道路交通流量的功能。整体上这是一个典型的现代城市街道场景，反映了日常生活中人们如何使用公共交通工具来移动和通勤。\"}]}}]}}";
       resizeImagesAll("E:\\workdata\\码诚科技\\动漫采集\\本地\\Object_3k","E:\\workdata\\码诚科技\\动漫采集");
//        String path = "E:\\workdata\\码诚科技\\采集翻译\\正式对接\\视频\\本地\\自有库人体视频样例";
//        delAlltxt(path);

//        String targetPath = "E:\\workdata\\码诚科技\\采集翻译\\正式对接\\王芳\\本地\\女人150";
////        delEnglist(targetPath);
//          collectionTranslate(targetPath);
//        zycjFormat(targetPath);
//        delAlltxt(targetPath);
    }
}